package com.timing.finder.framework;

import com.timing.finder.util.DateUtils;
import com.timing.finder.util.StringUtils;
import com.timing.finder.util.io.PropertiesHolder;

import java.text.ParseException;
import java.util.concurrent.TimeUnit;

/**
 * 默认的Yaml文件配置工厂
 *
 * @author JingGe(* ^ ▽ ^ *)
 * @date 2023-03-03 16:53
 * @email 1158055613@qq.com
 */
public class YamlConfigFactory implements TimingFinderConfigFactory {
    @Override
    public TimingFinderConfig create(String configSuffix) {
        String suffix = StringUtils.isEmpty(configSuffix) ? "" : "-" + configSuffix;
        PropertiesHolder propertiesHolder = PropertiesHolder
                .builder()
                .addPropertiesFile(String.format("timing-config%s.yml", suffix))
                .addPropertiesFile(String.format("timing-config%s.properties", suffix))
                .build();
        TimingFinderConfig config = new TimingFinderConfig();
        config.applicationName = propertiesHolder.getProperty("applicationName", "default");
        config.enableLoader = propertiesHolder.getProperty("loader.enable", Boolean.class, "true");
        config.cronExpression = propertiesHolder.getProperty("loader.cron", "0 0 0 * * ?");
        config.enableConcurrency = propertiesHolder.getProperty("loader.concurrency.enable", Boolean.class, "true");
        config.clearBeforeRun = propertiesHolder.getProperty("loader.clearBeforeRun", Boolean.class, "false");
        config.testAfterWhenError = propertiesHolder.getProperty("loader.testAfterWhenError", Boolean.class, "true");
        config.isCacheClustered = propertiesHolder.getProperty("loader.isCacheClustered", Boolean.class, "true");
        config.concurrencyThreadCount = propertiesHolder.getProperty("loader.concurrency.threadCount", Integer.class, "1");
        config.lowPowerCron = propertiesHolder.getProperty("loader.concurrency.lowPower.cron", "0 0 6 * * ?");
        config.repeatCount = propertiesHolder.getProperty("loader.repeatCount", Integer.class, "-1");
        config.lowPowerThreadCount = propertiesHolder.getProperty("loader.concurrency.lowPower.threadCount", Integer.class, "1");
        config.batchSize = propertiesHolder.getProperty("loader.batchSize", Integer.class, "10000");
        config.maximumExecutionTime = propertiesHolder.getProperty("loader.interrupt.maximumExecutionTime", Double.class, "350");
        config.maximumExecutionSize = propertiesHolder.getProperty("loader.interrupt.maximumExecutionSize", Long.class, "-1");
        config.starID = propertiesHolder.getProperty("loader.startID", Long.class, "0");
        config.endID = propertiesHolder.getProperty("loader.endID", Long.class, "-1");
        if (config.endID > 0 && config.endID < config.starID) {
            throw new IllegalArgumentException("截止ID不得小于起始ID");
        }
        config.initialSize = propertiesHolder.getProperty("bucket.initialSize", Long.class, "370");
        config.enableAlloc = propertiesHolder.getProperty("bucket,enableAlloc", Boolean.class, "true");
        config.expiringTime = propertiesHolder.getProperty("bucket.expiringTime", Integer.class, "366");
        config.bloomExpectedInsertions = propertiesHolder.getProperty("loader.bloom.expectedInsertions", Long.class, "100000");
        config.bloomMisjudgmentRate = propertiesHolder.getProperty("loader.bloom.misjudgmentRate", Double.class, "0.000001");
        try {
            config.defaultStartOffset = DateUtils
                    .parseDate(propertiesHolder.getProperty("handler.defaultStartOffset", DateUtils.getYear() + "-1-1"), "yyyy-MM-dd")
                    .getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        config.defaultUnitOffset = propertiesHolder.getProperty("handler.defaultUnitOffset", Long.class, "" + TimeUnit.DAYS.toMillis(1));
        return config;
    }
}
